//
//  BaseRequest+MoneyAndRemember.m
//  忘不了
//
//  Created by rimi on 16/7/22.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "BaseRequest+MoneyAndRemember.h"
#import <AVOSCloud/AVOSCloud.h>
#import "UserModel.h"
@implementation BaseRequest (MoneyAndRemember)
+ (void)startUpDataSourceWithFormName:(NSString *)formName dataSource:(NSArray *)dataSource completionhanlder:(RequestCompletion)callBack{
    AVQuery *query = [AVQuery queryWithClassName:formName];
    Response *response = [[Response alloc]init];
    UserModel *user = [UserModel shareUserModel];
    [query whereKey:@"userName" equalTo:user.userNmae];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSDictionary *dic = objects[0];
            NSArray *array = dic[@"content"];
            NSMutableArray *data = array.mutableCopy;
//            [data addObjectsFromArray:dic[@"content"]];
            [data addObjectsFromArray:dataSource];
            AVObject *todo =[AVObject objectWithClassName:formName objectId:dic[@"objectId"]];
            [todo setObject:data forKey:@"content"];
            [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    response.isSuccess = YES;
                }else{
                    response.isSuccess = NO;
                    NSLog(@"%@",error.localizedDescription);
                }
                callBack(response);
            }];
        }else{
            AVObject *todoFolder = [[AVObject alloc] initWithClassName:formName];// 构建对象
            [todoFolder setObject:user.userNmae forKey:@"userName"];
            [todoFolder setObject:dataSource forKey:@"content"];
            [todoFolder saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                }else{
                    NSLog(@"%@",error.localizedDescription);
                }
            }];
            callBack(response);
        }
        }];
}
+(void)startGetDataSourceWithForm:(NSString *)formName completionhanlder:(RequestCompletion)callBack{
    Response *response = [[Response alloc]init];
    UserModel *user = [UserModel shareUserModel];
    AVQuery *query = [AVQuery queryWithClassName:formName];
    [query whereKey:@"userName" equalTo:user.userNmae];
    [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
        NSArray<AVObject *> *todos = results;
        if (todos.count != 0) {
            response.isSuccess = YES;
            response.resultObject = todos[0][@"content"];
            NSLog(@"----%@",response.resultObject);
        }else{
            response.isSuccess = NO;
            response.resultObject = nil;
        }
        callBack(response);
    }];
}
//删除所有数据
+(void)removeDataWithUserName:(NSString *)userName formName:(NSString *)formName {
    AVQuery *query = [AVQuery queryWithClassName:formName];
    [query whereKey:@"userName" equalTo:userName];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSDictionary *dic = objects[0];
            NSArray *array = dic[@"content"];
            NSMutableArray *data = array.mutableCopy;
            data = @[].mutableCopy;
            AVObject *todo =[AVObject objectWithClassName:formName objectId:dic[@"objectId"]];
            [todo setObject:data forKey:@"content"];
            [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"保存成功");
                }else{
                    NSLog(@"%@",error.localizedDescription);
                }
            }];
        }
    }];
}
//删除某行数据
+(void)deleteDataWithFormName:(NSString *)formName index:(NSInteger)index completionhanlder:(RequestCompletion)callBack{
    AVQuery *query = [AVQuery queryWithClassName:formName];
    UserModel *user = [UserModel shareUserModel];
     Response *response = [[Response alloc]init];
    [query whereKey:@"userName" equalTo:user.userNmae];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSDictionary *dic = objects[0];
            NSArray *array = dic[@"content"];
            NSMutableArray *data = array.mutableCopy;
            [data removeObjectAtIndex:index];
            AVObject *todo =[AVObject objectWithClassName:formName objectId:dic[@"objectId"]];
            [todo setObject:data forKey:@"content"];
            [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    response.isSuccess = YES;
                }else{
                    NSLog(@"%@",error.localizedDescription);
                }
            }];
        }
        callBack(response);
    }];
}
//用户反馈的信息
+(void)getAdviceFromUserName:(NSString *)userName withDetail:(NSString *)detail completionhanlder:(RequestCompletion)callBack{
    AVObject *todoFolder = [[AVObject alloc] initWithClassName:@"Advice"];// 构建对象
    UserModel *user = [UserModel shareUserModel];
    Response *response = [[Response alloc]init];
    [todoFolder setObject:user.userNmae forKey:@"userName"];
    [todoFolder setObject:detail forKey:@"advice"];
    [todoFolder saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            response.isSuccess = YES;
        } else {
            response.isSuccess = NO;
        }
        callBack(response);
    }];
}
//修改邮箱
+(void)updateEmailWithUserName:(NSString *)userName password:(NSString *)password NewEmail:(NSString *)email completionhanlder:(RequestCompletion)callBack{
    UserModel *user = [UserModel shareUserModel];
    Response *response = [[Response alloc]init];
    if ([userName isEqualToString:user.userNmae] && [password isEqualToString:user.password]) {
        AVQuery *query = [AVQuery queryWithClassName:@"DefaultUser"];
        [query whereKey:@"userNmae" equalTo:user.userNmae];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (objects.count > 0) {
                NSDictionary *dic = objects[0];
                AVObject *todo =[AVObject objectWithClassName:@"DefaultUser" objectId:dic[@"objectId"]];
                [todo setObject:email forKey:@"email"];
                [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (error == nil) {
                        response.isSuccess = YES;
                        NSLog(@"修改成功");
                    }else{
                        response.isSuccess = NO;
                        NSLog(@"修改失败");
                    }
                    callBack(response);
                }];
            }
        }];
    }else{
        NSLog(@"账号或密码错误");
    }
    
}
//上传头像
+(void)updateHeadImageFromUserName:(NSString *)imageUrl{
    AVQuery *query = [AVQuery queryWithClassName:@"HeaderImage"];
    UserModel *user = [UserModel shareUserModel];
    [query whereKey:@"userName" equalTo:user.userNmae];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSDictionary *dic = objects[0];
            AVObject *todo =[AVObject objectWithClassName:@"HeaderImage" objectId:dic[@"objectId"]];
            [todo setObject:imageUrl forKey:@"imageStirng"];
            [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    NSLog(@"保存成功");
                }
            }];
        }else{
            AVObject *todoFolder = [[AVObject alloc] initWithClassName:@"HeaderImage"];// 构建对象
            [todoFolder setObject:user.userNmae forKey:@"userName"];
            [todoFolder setObject:imageUrl forKey:@"imageStirng"];
            [todoFolder saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    NSLog(@"保存成功");
                }
            }];
        }
    }];
}
//获取头像
+(void)getHeadImageFromUserName:(NSString *)userName completionhanlder:(RequestCompletion)callBack{
    Response *response = [[Response alloc]init];
    AVQuery *query = [AVQuery queryWithClassName:@"HeaderImage"];
    UserModel *user = [UserModel shareUserModel];
    if (user.isLogin) {
        [query whereKey:@"userName" equalTo:userName];
        [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
            NSArray<AVObject *> *todos = results;
            if (todos.count != 0) {
                response.isSuccess = YES;
                response.resultObject = todos[0][@"imageStirng"];
            }else{
                response.isSuccess = NO;
                response.resultObject = nil;
            }
            callBack(response);
        }];

    }else{
        return;
  }
}

@end
